<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\controller\ai;


use app\api\controller\BaseApiController;
use GuzzleHttp\Client;


/**
 * openai
 * Class OpenAiController
 * @package app\api\controller\gpt
 */
class OpenAiTestController extends BaseApiController
{

    public array   $notNeedLogin = ['hello', 'getModelList', 'retrieveSession', 'hello2'];
    private string $api_host     = 'https://openai.allwin.top';
    private Client $HttpClient;
    private string $api_key;

    public function initialize()
    {
        parent::initialize();
        $this->HttpClient = new Client([
            'base_uri' => $this->api_host,
            'timeout'  => 5,
        ]);
        $this->api_key    = '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';

    }

    // http://loc.gpt.com/api/gpt.open_ai_test/hello
    public function hello()
    {
        try {
            // 设置请求的授权信息
            $headers = [
                'Authorization' => 'Bearer ' . $this->api_key,
            ];
            $option  = [
                'query'   => false,
                'verify'  => false,
                'headers' => $headers,
                'json'    => [
                    'model'    => 'gpt-3.5-turbo',
                    'messages' => [
                        ['role' => 'user', 'content' => '我个子高吗？'],
                    ],
                ]
            ];

            $response = $this->HttpClient->request('POST', '/v1/chat/completions', $option);
            $body     = $response->getBody()->getContents();
            dd($body);
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }


}